"""用户视图层"""

from core import admin_view
from lib import common
from interface import user_interface, atm_interface, shop_interface

login_username = ''


def register():
    """注册功能视图层函数"""
    while True:
        username = input('请设置用户名：').strip()
        password = input('请设置登录密码：').strip()
        confirm_password = input('请确认密码：').strip()
        if not (username and password):
            print('用户名和密码不能为空!')
            continue
        elif not (password == confirm_password):
            print('两次密码不一致，请重新输入！')
            continue
        flag, prompt_msg = user_interface.register_interface(username, password)
        if flag:
            print(prompt_msg)
            break
        else:
            print(prompt_msg)
    return None


def login():
    """登录功能视图层函数"""
    while True:
        username = input('请输入用户名：').strip()
        password = input('请输入登录密码：').strip()
        if not (username and password):
            print('用户名和密码不能为空！')
            continue
        flag, prompt_msg = user_interface.login_interface(username, password)
        if flag:
            global login_username
            login_username = username
            print(prompt_msg)
            break
        else:
            print(prompt_msg)
    return None


@common.login_auth
def check_balance():
    """查看余额功能视图层函数"""
    result = atm_interface.check_balance_interface(login_username)
    print(result)
    return None


@common.login_auth
def withdraw():
    """提现功能视图层函数"""
    while True:
        withdraw_deposit = float(input('请输入提现金额：').strip())
        flag, prompt_msg = atm_interface.withdraw_interface(
            login_username,
            withdraw_deposit,
        )
        if flag:
            print(prompt_msg)
            break
        else:
            print(prompt_msg)
    return None


@common.login_auth
def repay():
    """还款功能视图层函数"""
    repayment = float(input('请输入还款金额：').strip())
    result = atm_interface.repay_interface(login_username, repayment)
    print(result)
    return None


@common.login_auth
def transfer():
    """转账功能视图层函数"""
    while True:
        money = float(input('请输入转账金额：').strip())
        to_username = input('请输入对方账户名称：').strip()
        flag, prompt_msg = atm_interface.transfer_interface(login_username, to_username, money)
        if flag:
            print(prompt_msg)
            break
        else:
            print(prompt_msg)
    return None


@common.login_auth
def check_flow():
    """查看流水功能视图层函数"""
    flow = atm_interface.check_flow_interface(login_username)
    if not flow:
        print('该用户暂时没有流水。')
    for i in flow:
        print(i)
    return None


@common.login_auth
def shop():
    """购物功能视图层函数"""
    while True:
        # 1 展示商品列表
        phone_dict = common.get_phone()
        for index, phone in phone_dict.items():
            print("商品编号[{0}] 商品名称[{name}] 商品单价[{price}]".format(index, **phone))
        # 2 让用户选择商品加入购物车
        number = input('请输入商品编号：').strip()
        if number not in phone_dict:
            print('请输入正确的编号：')
            continue
        phone = phone_dict.get(number)
        shopping_cart = shop_interface.add_to_cart_interface(login_username, phone)
        # 3 展示购物车
        print(shopping_cart)
        # 4 询问用户是否继续购物
        flag = input('是否继续购物？y or n：'.strip())
        if flag == 'n':
            break
    return None


@common.login_auth
def pay():
    """结账功能视图层函数"""
    prompt_msg = shop_interface.pay_interface(login_username)
    print(prompt_msg)
    return None


@common.login_auth
def admin():
    """管理员功能"""
    if login_username == 'root':
        admin_view.main()
    else:
        print('你没有管理员权限。')
    return None


number_of_func = {
    '1': register,
    '2': login,
    '3': check_balance,
    '4': withdraw,
    '5': repay,
    '6': transfer,
    '7': check_flow,
    '8': shop,
    '9': pay,
    '10': admin,
}


def main():
    """用户视图层主函数"""
    while True:
        print("""             
            欢迎来到ATM项目！
            0  退出
            1  注册功能
            2  登录功能
            3  查看余额
            4  提现功能
            5  还款功能
            6  转账功能
            7  查看流水
            8  购物功能
            9  结账功能
            10 管理员功能""")
        number = input('请输入功能编号：').strip()
        if number == '0':
            print('退出程序！')
            break
        elif number not in number_of_func:
            print('请输入正确的编号！')
            continue
        number_of_func[number]()
    return None


if __name__ == '__main__':
    print(main.__doc__)
    print(__doc__)
